<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;
use app\controller\wmapi\IndexController;
use support\Request;

class WaPurchaseOrdersExamineApprove
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_purchase_order_examine_approve';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    public static function list($param, $adminId, $page, $pageNum, $whereNotIn, $purchaseOrdersn, $fields = ['*'], $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        // $purchaseOrdersn 为空 就是没有当前登录用户的审核订单
        // 数组转对象  get_object_vars($purchaseOrdersn)
        if (gettype($purchaseOrdersn) == "object") {
            $purchaseOrdersn = get_object_vars($purchaseOrdersn); // 对象转数组函数
        } elseif (gettype($purchaseOrdersn) == "array") {
            $purchaseOrdersn = $purchaseOrdersn;
        }
        $condition [] = ['is_show','=',1];
        /**
         * 先获取审批中 待审批的 订单编号 和对应的审批id或编号
         * 审批流中的 部门id 必须是 当前人员的部门
         * 遍历审批流中的 部门admin_ids 中必须有当前人员 有他 但是并没有审批通过的状态就需要展示（因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录）
         *
         */
        $wai_approve_sn = []; // 待当前人员审批的订单
        $where_approve = [];
        $where_approve [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号

        $approve = WaPurchaseOrdersExamineApprove::all($where_approve);
        $approve = json_decode($approve);
        if ($approve) {
            foreach ($approve as $kap) {
                $where_approve_flow = [];
                $where_approve_flow [] = ['department_id', '=', $adminId->department_id]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                $where_approve_flow [] = ['po_no', '=', $kap->po_no];
                //先判断 是否有 通过的  撤回  驳回的 不用考虑 因为上一步筛选的就是待审批中的审批订单
                $where_approve_flow [] = ['status', '=', 1]; // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录
                $approve_flow = WaPurchaseOrdersExamineApproveFlow::all($where_approve_flow);
                $approve_flow = json_decode($approve_flow);
                if (!$approve_flow) {
                    // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录  只要排除 有人审核通过 且本人没有审核过 就是本人待审核
                    $where_approve_flow_0 = [];
                    $where_approve_flow_0 [] = ['department_id', '=', $adminId->department_id]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                    $where_approve_flow_0 [] = ['po_no', '=', $kap->po_no];
                    $where_approve_flow_0 [] = ['status', '=', 0]; // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录
                    $approve_flow_0 = WaPurchaseOrdersExamineApproveFlow::all($where_approve_flow_0);
                    $approve_flow_0 = json_decode($approve_flow_0);
                    if ($approve_flow_0) {
                        foreach ($approve_flow_0 as $kapf0) {
                            // 筛出来 还要加一层保险 确保 部门中审核人员 由本人
                            if ($kapf0->admin_ids) {
                                $admin_ids = json_decode($kapf0->admin_ids);
                                if (in_array($adminId->id, $admin_ids)) {
                                    // 这个时候把审批订单编号扔出来 就是待当前人员待审核的数据
                                    array_push($wai_approve_sn, $kap->po_no);
                                }
                            }
                        }
                    }
                }
            }
        }

        if ($param['status'] == 4) {
            if ($between) {
                $model = Db::table('wa_purchase_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('po_no', $wai_approve_sn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_purchase_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('po_no', $wai_approve_sn);
            }
        } else {
            if ($between) {
                $model = Db::table('wa_purchase_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNotIn('status', $whereNotIn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_purchase_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNotIn('status', $whereNotIn);
            }
        }

        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();

        if ($list) {
            foreach ($list as $k) {
                if ($k->po_no) {
                    // 获取订单的 对应的审批编号
                    $orders = WaOrdersPurchase::find(['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $k->po_no,]);
                    $orders = json_decode($orders);
                    // 判断 是审批流页面进入的查看 还是 订单页面进入的 查看
                    if ($orders) {
                        // 订单页面进入的
                        $approve = WaPurchaseOrdersExamineApproveFlow::all(
                            ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no]);
                        $approve = json_decode($approve);
                        if ($approve) {
                            foreach ($approve as $kpr) {
                                if ($kpr->admin_ids){
                                    $kpr->admin_ids = json_decode($kpr->admin_ids);
                                }
                                if ($kpr->status == 3) {
                                    $approve_status = 3; // 已驳回
                                    $approve_status_name = '已撤回';
                                } elseif ($kpr->status == 2) {
                                    $approve_status = 2; // 已驳回
                                    $approve_status_name = '已驳回';
                                } elseif ($kpr->status == 1) {
                                    if (in_array($adminId->id, $kpr->admin_ids)){
                                        $approve_status = 1; // 已审批
                                        $approve_status_name = '已审批';
                                    }else{
                                        $approve_status = 998; // 审批中
                                        $approve_status_name = '审批中';
                                    }

                                } elseif ($kpr->status == 0) {
                                    if (in_array($adminId->id, $kpr->admin_ids)){
                                        $approve_status = 0; // 待审批
                                        $approve_status_name = '待审批';
                                    }else{
                                        $approve_status = 998; // 审批中
                                        $approve_status_name = '审批中';
                                    }

                                } else {
                                    $approve_status = 999; // 已驳回
                                    $approve_status_name = '不在审批流中';
                                }
                            }
                        } else {
                            $approve_status = 10001; // 审批状态
                            $approve_status_name = '没有审批记录';// 审批状态名称
                        }


                    } else {
                        $approve_status = 10001; // 审批状态
                        $approve_status_name = '待提交审批';// 审批状态名称
                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '待提交审批';// 审批状态名称
                }

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                $k->approve_flow = json_decode($k->approve_flow);
                // 采购单审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批通过';
                } elseif ($k->status == 2) {
                    $k->status_name = '审批驳回';
                } elseif ($k->status == 3) {
                    $k->status_name = '撤回审批';
                } elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                } elseif ($k->status == 5) {
                    $k->status_name = '作废';
                } else {
                    $k->status_name = '';
                }
//                $k->approve_flow = $k->approve_flow;

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    /**
     *轮到我审批的订单
     * 先判断 是否有 通过的  撤回  驳回的 不用考虑 因为上一步筛选的就是待审批中的审批订单
     * 但是待审批中的审批订单也存在审批未完成 就被删除 作废 撤回的情况
     * 最后的解决方法是：
     * 筛选所有本人的的待审批订单  即 状态为 0 的   然后根据对应的订单编号 去查询对应的订单 当前的状态是什么 必须是待审批状态才展示
     * @param $adminId
     * @param $page
     * @param $pageNum
     * @param $purchaseOrdersn
     * @param array $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */

    public static function waitApproveByAdminIdList($adminId, $page, $pageNum, $purchaseOrdersn, $fields = ['*'], $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        // $purchaseOrdersn 为空 就是没有当前登录用户的审核订单
        // 数组转对象  get_object_vars($purchaseOrdersn)
        if (gettype($purchaseOrdersn) == "object") {
            $purchaseOrdersn = get_object_vars($purchaseOrdersn); // 对象转数组函数
        } elseif (gettype($purchaseOrdersn) == "array") {
            $purchaseOrdersn = $purchaseOrdersn;
        }
        $condition [] = ['is_show','=',1];
        /**
         * 先获取审批中 待审批的 订单编号 和对应的审批id或编号
         * 审批流中的 部门id 必须是 当前人员的部门
         * 遍历审批流中的 部门admin_ids 中必须有当前人员 有他 但是并没有审批通过的状态就需要展示（因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录）
         *
         */
        $wai_approve_sn = []; // 待当前人员审批的订单
        $where_approve = [];
        $where_approve [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号

        $approve = WaPurchaseOrdersExamineApprove::all($where_approve);
        $approve = json_decode($approve);

        if ($approve) {
            foreach ($approve as $kap) {
                $where_order = [];
                $where_order [] = ['purchase_order_sn', '=', $kap->purchase_order_sn];
                $order_data = WaOrdersPurchase::find($where_order);
                $order_data = json_decode($order_data);
                if ($order_data) {
                    PublicCommentController::采购订单状态;
                    // 主订单是审批中的 才需要展示在待审批列表
                    if ($order_data->purchase_orders_status == 1) {
                        // 现在开始查审批流
                        $where_approve_flow = [];
                        $where_approve_flow [] = ['po_no', '=', $kap->po_no]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                        $where_approve_flow [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                        $approve_flow = WaPurchaseOrdersExamineApproveFlow::find($where_approve_flow);
                        $approve_flow = json_decode($approve_flow);
                        if ($approve_flow) {
                            if ($approve_flow->admin_ids)
                            {
                                $admin_ids = json_decode($approve_flow->admin_ids);
                                if (in_array($adminId->id, $admin_ids)) {
                                    // 这个时候把审批订单编号扔出来 就是待当前人员待审核的数据
                                    array_push($wai_approve_sn, $kap->po_no);
                                }
                            }
                        }

                    }
                }
            }
        }



        $model = Db::table('wa_purchase_order_examine_approve')
            ->select($fields)
            ->where($condition)
            ->whereIn('po_no', $wai_approve_sn);
        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();

        if ($list) {
            foreach ($list as $k) {


                // 获取订单的 对应的审批编号
                $orders = WaOrdersPurchase::find(['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $k->po_no,]);
                $orders = json_decode($orders);
                // 判断 是审批流页面进入的查看 还是 订单页面进入的 查看
                if ($orders) {
                    // 订单页面进入的
                    if ($orders->po_no) {
                        $approve = WaPurchaseOrdersExamineApproveFlow::all(
                            ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no]);
                        $approve = json_decode($approve);
                        if ($approve) {
                            foreach ($approve as $kpr) {
                                if ($kpr->admin_ids){
                                    $kpr->admin_ids = json_decode($kpr->admin_ids);
                                }
                                if ($kpr->status == 3) {
                                    $approve_status = 3; // 已驳回
                                    $approve_status_name = '已撤回';
                                } elseif ($kpr->status == 2) {
                                    $approve_status = 2; // 已驳回
                                    $approve_status_name = '已驳回';
                                } elseif ($kpr->status == 1) {
                                    if (in_array($adminId->id, $kpr->admin_ids)){
                                        $approve_status = 1; // 已审批
                                        $approve_status_name = '已审批';
                                    }else{
                                        $approve_status = 998; // 审批中
                                        $approve_status_name = '审批中';
                                    }

                                } elseif ($kpr->status == 0) {
                                    if (in_array($adminId->id, $kpr->admin_ids)){
                                        $approve_status = 0; // 待审批
                                        $approve_status_name = '待审批';
                                    }else{
                                        $approve_status = 998; // 审批中
                                        $approve_status_name = '审批中';
                                    }

                                } else {
                                    $approve_status = 999; // 已驳回
                                    $approve_status_name = '不在审批流中';
                                }
                            }
                        } else {
                            $approve_status = 10001; // 审批状态
                            $approve_status_name = '没有审批记录';// 审批状态名称
                        }


//                        $approve = WaPurchaseOrdersExamineApproveFlow::all(
//                            ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no, 'status' => 3]);
//                        $approve = json_decode($approve);
//                        if ($approve) {
//                            $approve_status = 3; // 已撤回
//                            $approve_status_name = '已撤回';
//
//                        } else {
//                            $approve = WaPurchaseOrdersExamineApproveFlow::all(
//                                ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no, 'department_id' => $adminId->department_id, 'status' => 2]);
//                            $approve = json_decode($approve);
//                            if ($approve) {
//                                $approve_status = 2; // 已驳回
//                                $approve_status_name = '已驳回';
//                            } else {
//                                $approve = WaPurchaseOrdersExamineApproveFlow::all(
//                                    ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no, 'department_id' => $adminId->department_id, 'status' => 1]);
//                                $approve = json_decode($approve);
//                                if ($approve) {
//                                    $approve_status = 1; // 已审批
//                                    $approve_status_name = '已审批';
//                                } else {
//                                    $approve = WaPurchaseOrdersExamineApproveFlow::all(
//
//                                        ['purchase_order_sn' => $k->purchase_order_sn, 'po_no' => $orders->po_no, 'department_id' => $adminId->department_id, 'status' => 0]);
//                                    if ($orders->po_no == 'POOZF558') {
//
//                                    }
//
//                                    $approve = json_decode($approve);
//                                    if ($approve) {
//                                        $approve_status = 0; // 待审批
//                                        $approve_status_name = '待审批';
//                                    } else {
//                                        $approve_status = 999;// 待提交审批
//                                        $approve_status_name = '待提交审批';
//                                    }
//                                }
//                            }
//
//                        }


                        $k->approve_status = $approve_status; // 审批状态
                        $k->approve_status_name = $approve_status_name;// 审批状态名称
                    } else {
                        echo '数据有误';
                    }

                } else {
                    $k->approve_status = 999; // 审批状态
                    $k->approve_status_name = '待提交审批';// 审批状态名称
                }


                $k->approve_flow = json_decode($k->approve_flow);
                // 采购单审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批通过';
                } elseif ($k->status == 2) {
                    $k->status_name = '审批驳回';
                } elseif ($k->status == 3) {
                    $k->status_name = '撤回审批';
                } elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                } elseif ($k->status == 5) {
                    $k->status_name = '作废';
                } else {
                    $k->status_name = '';
                }
//                $k->approve_flow = $k->approve_flow;

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 采购订单审批 -创建
     * @param $data
     * @return int
     */
    public static function create($data)
    {
        return Db::table('wa_purchase_order_examine_approve')->insertGetId($data);
    }


    /**
     * 采购订单审批-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition, $data)
    {
        return Db::table('wa_purchase_order_examine_approve')->where($condition)->update($data);
    }

    /**
     * 采购订单审批-查询
     * @param $data
     * @return int
     */
    public static function find($adminId, $condition)
    {
        $res = Db::table('wa_purchase_order_examine_approve')
            ->where($condition)
            ->first();
        if ($res) {
            // 判断 是审批流页面进入的查看 还是 订单页面进入的 查看
                    $approve = WaPurchaseOrdersExamineApproveFlow::all(
                        ['purchase_order_sn' => $res->purchase_order_sn, 'po_no' => $res->po_no]);
                    $approve = json_decode($approve);

                    if ($approve) {
                        foreach ($approve as $kpr) {
                            if ($kpr->admin_ids){
                                $kpr->admin_ids = json_decode($kpr->admin_ids);
                            }
                            if ($kpr->status == 3) {
                                $approve_status = 3; // 已驳回
                                $approve_status_name = '已撤回';
                            } elseif ($kpr->status == 2) {
                                $approve_status = 2; // 已驳回
                                $approve_status_name = '已驳回';
                            } elseif ($kpr->status == 1) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 1; // 已审批
                                    $approve_status_name = '已审批';
                                } else {
                                    $approve_status = 1; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } elseif ($kpr->status == 0) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 0; // 待审批
                                    $approve_status_name = '待审批';
                                } else {
                                    $approve_status = 0; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } else {
                                $approve_status = 999; // 已驳回
                                $approve_status_name = '不在审批流中';
                            }
                        }

                    $res->approve_status = $approve_status; // 审批状态
                    $res->approve_status_name = $approve_status_name;// 审批状态名称
                } else {
                    echo '数据有误';
                }
        }
        return json_encode($res);
    }


    public static function all($condition)
    {
        $pageNum = 10;
        $condition [] = ['is_show','=',1];
        $list = Db::table('wa_purchase_order_examine_approve')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }

}